<template>
  <a-card :bordered="false">
    <a-row :gutter="8">
      <a-col :span="4">
        <s-tree
          :dataSource="orgTree"
          :openKeys.sync="openKeys"
          :search="false"
          @click="handleClick"
          @titleClick="handleTitleClick"></s-tree>
      </a-col>
      <a-col :span="20">
        <s-table
          ref="table"
          size="default"
          :columns="columns"
          :data-source="dataSource"
          :alert="false"
          :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        >
          <span slot="action" slot-scope="text, record">
            <template v-if="$auth('table.update')">
              <a @click="handleEdit(record)">编辑</a>
              <a-divider type="vertical" />
            </template>
            <a-dropdown>
              <a class="ant-dropdown-link">
                更多 <a-icon type="down" />
              </a>
              <a-menu slot="overlay">
                <a-menu-item>
                  <a href="javascript:;">详情</a>
                </a-menu-item>
                <a-menu-item v-if="$auth('table.disable')">
                  <a href="javascript:;">禁用</a>
                </a-menu-item>
                <a-menu-item v-if="$auth('table.delete')">
                  <a href="javascript:;">删除</a>
                </a-menu-item>
              </a-menu>
            </a-dropdown>
          </span>
        </s-table>
      </a-col>
    </a-row>

    <org-modal ref="modal" @ok="handleSaveOk" @close="handleSaveClose" />
  </a-card>
</template>

<script>
import STree from '@/components/Tree/Tree'
import { STable } from '@/components'
import OrgModal from '../other/modules/OrgModal'
import { getServiceList } from '@/api/manage'

export default {
  name: 'TreeList',
  components: {
    STable,
    STree,
    OrgModal
  },
  data () {
    return {
      openKeys: ['key-01'],
      // 查询参数
      queryParam: {},
      dataSource: [
        {
          no: 1,
          description: '666',
          状态: '状态'
        }
      ],
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: 'no'
        },
        {
          title: '成员名称',
          dataIndex: 'description'
        },
        {
          title: '登录次数',
          dataIndex: 'callNo',
          sorter: true,
          needTotal: true,
          customRender: (text) => text + ' 次'
        },
        {
          title: '状态',
          dataIndex: '状态',
          needTotal: true
        },
        {
          title: '更新时间',
          dataIndex: 'updatedAt',
          sorter: true
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '150px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getServiceList(Object.assign(parameter, this.queryParam))
          .then(res => {
            console.log(parameter)
            return res.result
          })
      },
      orgTree: [{
        'key': 'key-01',
        'title': '脚架模拆',
        'icon': 'mail'
        // 'children': [{
        //   'key': 'key-01-01',
        //   'title': '后端组',
        //   'icon': null,
        //   'group': true,
        //   children: [{
        //     'key': 'key-01-01-01',
        //     'title': 'JAVA',
        //     'icon': null
        //   },
        //   {
        //     'key': 'key-01-01-02',
        //     'title': 'PHP',
        //     'icon': null
        //   },
        //   {
        //     'key': 'key-01-01-03',
        //     'title': 'Golang',
        //     'icon': null
        //   }
        //   ]
        // }, {
        //   'key': 'key-01-02',
        //   'title': '前端组',
        //   'icon': null,
        //   'group': true,
        //   children: [{
        //     'key': 'key-01-02-01',
        //     'title': 'React',
        //     'icon': null
        //   },
        //   {
        //     'key': 'key-01-02-02',
        //     'title': 'Vue',
        //     'icon': null
        //   },
        //   {
        //     'key': 'key-01-02-03',
        //     'title': 'Angular',
        //     'icon': null
        //   }
        //   ]
        // }]
      }, {
        'key': 'key-02',
        'title': '起重吊装',
        'icon': 'dollar',
        'children': [{
          'key': 'key-02-01',
          'title': '会计核算',
          'icon': null
        }, {
          'key': 'key-02-02',
          'title': '成本控制',
          'icon': null
        }, {
          'key': 'key-02-03',
          'title': '内部控制',
          'icon': null,
          'children': [{
            'key': 'key-02-03-01',
            'title': '财务制度建设',
            'icon': null
          },
          {
            'key': 'key-02-03-02',
            'title': '会计核算',
            'icon': null
          }
          ]
        }]
      }],
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  created () {
    // getOrgTree().then(res => {
    //   this.orgTree = res.result
    // })
  },
  methods: {
    handleClick (e) {
      console.log('handleClick', e)
      this.queryParam = {
        key: e.key,
        pageNo: 1
      }
      console.log(this.queryParam)
      this.$refs.table.refresh(true)
    },
    handleAdd (item) {
      console.log('add button, item', item)
      this.$message.info(`提示：你点了 ${item.key} - ${item.title} `)
      this.$refs.modal.add(item.key)
    },
    handleTitleClick (item) {
      console.log('handleTitleClick', item)
    },
    titleClick (e) {
      console.log('titleClick', e)
    },
    handleSaveOk () {

    },
    handleSaveClose () {

    },
    // handleChange (pagination) {
    //   this.queryParam.pageNo = pagination.current
    //   this.queryParam.pageSize = pagination.pageSize
    //   console.log('????')
    //   this.$refs.table.refresh(true)
    // },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    }
  }
}
</script>

<style lang="less">
  .custom-tree {

    /deep/ .ant-menu-item-group-title {
      position: relative;
      &:hover {
        .btn {
          display: block;
        }
      }
    }

    /deep/ .ant-menu-item {
      &:hover {
        .btn {
          display: block;
        }
      }
    }

    // /deep/ .ant-menu-submenu-title {
    //   width: 100px;
    // }

    /deep/ .btn {
      display: none;
      position: absolute;
      top: 0;
      right: 10px;
      width: 20px;
      height: 40px;
      line-height: 40px;
      z-index: 1050;

      &:hover {
        transform: scale(1.2);
        transition: 0.5s all;
      }
    }
  }
</style>
